package com.stack.knowyouservice.service.impl;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyouservice.service.FileService;
import com.stack.knowyoubase.util.AliYunOssUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 文件上传服务层
 *
 * @author stack
 */
@Service
@Scope("singleton")
public class FileServiceImpl implements FileService {

    @Resource
    private AliYunOssUtil aliYunOssUtil;

    public String createFileName(String rootDir, String fileName, String userId) {
        //获取带点的文件后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = format.format(new Date());
        return rootDir + userId + "-" + dateStr + "-" + UUID.randomUUID().toString() + suffix;
    }


    @Override
    public String imgUpload(MultipartFile file, Integer sid, Authentication authentication) {
        File newFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
        String fileUrl;
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
            String fileName = createFileName(sid + GlobalConst.HOMEWORK_IMG, file.getOriginalFilename(), authentication.getName());
            fileUrl = aliYunOssUtil.upLoadFile(newFile, fileName);
        } catch (Exception e) {
            return null;
        } finally {
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return fileUrl;
    }

    @Override
    public String videoUpload(MultipartFile file, Integer sid) {
        File newFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
        String fileUrl;
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
            String fileName = createFileName(sid + GlobalConst.VIDEO_FILE, file.getOriginalFilename(), sid.toString());
            fileUrl = aliYunOssUtil.upLoadFile(newFile, fileName);
        } catch (Exception e) {
            return null;
        } finally {
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return fileUrl;
    }

    @Override
    public String avatarUpload(MultipartFile file, String userId) {
        File newFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
        String fileUrl;
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
            String fileName = createFileName(GlobalConst.AVATAR_IMG, file.getOriginalFilename(), userId);
            fileUrl = aliYunOssUtil.upLoadFile(newFile, fileName);
        } catch (Exception e) {
            return null;
        } finally {
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return fileUrl;
    }


}
